package com.lht.arithmetic.sort;

import org.junit.Test;

import java.util.Arrays;
import java.util.Random;

/**
 * @author Eric
 * @version 1.0
 * @date 2020-05-25
 */
public class SortDemo {

    @Test
    public void bubbleSort() {
        compare(new BubbleSort(), randomCreateArr(9999));
    }

    @Test
    public void InsertionSort() {
        compare(new InsertionSort(), randomCreateArr(9999));
    }

    private void compare(Sort sort, int[] arr) {
        System.out.println(Arrays.toString(arr));

        int[] mySorts = new int[arr.length];
        System.arraycopy(arr, 0, mySorts, 0, arr.length);

        long s = System.currentTimeMillis();
        sort.sort(mySorts, 0, arr.length);
        long e = System.currentTimeMillis();
        System.out.println("自定义排序耗时：" + (e - s));

        s = System.currentTimeMillis();
        Arrays.sort(arr);
        e = System.currentTimeMillis();
        System.out.println("sort排序耗时：" + (e - s));

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != mySorts[i]) {
                System.out.println("排序错误,正确的：" + arr[i] + "，我的：" + mySorts[i]);
            }
        }
        System.out.println(Arrays.toString(mySorts));
        System.out.println(Arrays.toString(arr));
    }

    private static int[] randomCreateArr(int len) {
        if (len < 1) {
            return new int[0];
        }
        int[] arr = new int[len];
        Random random = new Random();
        for (int i = 0; i < len; i++) {
            arr[i] = random.nextInt(9999);
        }
        return arr;
    }
}
